package priv.winnine.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import priv.winnine.entity.bo.Constant;
import priv.winnine.entity.enums.ResultEnum;
import priv.winnine.entity.vo.Response;
import priv.winnine.entity.vo.TestEntity;
import priv.winnine.exception.ResultException;
import priv.winnine.service.TestService;
import priv.winnine.util.BeanConvertUtil;
import priv.winnine.util.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Validator;

@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private Validator validator;

    @PostMapping(
            value = "/test",
            consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE },
            produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
    public Response test(HttpServletRequest request) {
        String requestString = (String) request.getAttribute(Constant.HTTP_REQUEST_STRING);
        TestEntity entity = BeanConvertUtil.convertBean(validator, requestString, TestEntity.class);
        testService.test(entity);
        throw new ResultException(ResultEnum.SUCCESS);
        // return ResultUtil.success();
    }

}
